const { app, BrowserWindow, Menu, globalShortcut,  ipcMain } = require("electron");
const { resolve, dirname } = require("path");
const Store = require("electron-store");
const mkdirp = require("mkdirp");
const path = require("path");
const store = new Store();
const menu = Menu.buildFromTemplate([]);
Menu.setApplicationMenu(menu);
var exePath = dirname(app.getPath("exe"));
let workdirPath = resolve(exePath, "workdir");
mkdirp.sync(workdirPath);
store.set("workdir", workdirPath);


//获取单例锁
const gotTheLock = app.requestSingleInstanceLock();
// 自定义菜单

if (!gotTheLock) {
  app.quit();
} else {
  let mainWindow;
  async function createWindow() {
    const win = new BrowserWindow({
      width: 800,
      height: 500,
      // resizable: false,
      //useContentSize: true,
      /* transparent: true, */
      icon: path.join(__dirname, "view/favicon.ico"),
      show: true,
      webPreferences: {
        enableRemoteModule: false,
        webSecurity: false,
        nodeIntegration: true,
        contextIsolation: true,
        webviewTag: true,
        preload: path.join(__dirname, "src/preload.js"),
      },
    });
    require("./src/index")(win);


    await win.loadFile(path.join(__dirname, "view/index.html"));
    globalShortcut.register("Alt+f", function () {
      win.webContents.openDevTools();
    });


  


    return win
  }
  app.on("ready", async function () {
    mainWindow = await createWindow();
   
  });
  app.on("activate", async function () {
    mainWindow.focus();
  });
  app.on("second-instance", () => {
    if (mainWindow) {
      mainWindow.focus();
      if (mainWindow.isMinimized()) mainWindow.restore();
    }
  });


	app.on("activate", async function () {
		if (mainWindow) {
			mainWindow.show();
		}
	});
	// activate
	app.on("second-instance", () => {
		if (mainWindow) {
			if (mainWindow.isMinimized()) mainWindow.restore();
			mainWindow.focus();
		}
	});

	app.on("quit", () => {
		app.releaseSingleInstanceLock(); //释放所有的单例锁
	});
}


